﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AfterlifeLib.Particles
{
    public class Particle
    {
        public bool Active { get; set; }
        public Vector2 Position { get; set; }
        public Vector2 Velocity { get; set; }
        public Color Color { get; set; }
        public float Angle { get; set; }
        public float Scale { get; set; }
        public float Speed { get; set; }
        public Texture2D Texture { get; set; }
        public float LifeSpan { get; set; }
        public float Age { get; set; }
        public float MinAlpha { get; set; }
        public float Alpha { get { return (1f - Age / LifeSpan); } }

        public Particle() { }
        //add MinAlpha argument
        public void Initialize(Texture2D texture, Vector2 position, Color color, float speed, float angle, float scale, float lifeSpan)
        {
            Texture = texture;
            Position = position;
            Color = color;
            Velocity = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
            Angle = angle;
            Scale = scale;
            LifeSpan = lifeSpan;
            Speed = speed;
            Age = 0.0f;

            Active = true;
        }

        public void Update(float delta)
        {
            Age += delta;
            Position += (Velocity * Speed) * delta;

            if (Age > LifeSpan)
                Active = false;
        }

        public void Draw(SpriteBatch spriteBatch, Camera2D camera)
        {
            Vector2 screenPos = Position - camera.Position;
            if (screenPos.X < 0 || screenPos.X > Camera2D.ScreenWidth ||
                screenPos.Y < 0 || screenPos.Y > Camera2D.ScreenHeight)
                return;

            Vector2 origin = new Vector2(Texture.Width / 2, Texture.Height / 2);

            spriteBatch.Draw(Texture, screenPos, null, new Color(Color.R, Color.G, Color.B, Alpha), Angle, origin, Scale, SpriteEffects.None, 0);
        }
    }
}
